package org.lanqiao.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.lanqiao.entity.AcActivity;
import org.lanqiao.entity.Activity;
import org.lanqiao.service.ActivityService;
import org.lanqiao.util.Result;
import org.lanqiao.util.ResultUtil;
import org.lanqiao.util.UUId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 李静
 * @describe 获取活动管理表的数据
 * @data 2019/5/24
 */
@Controller
@ResponseBody
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    /**
     * @return org.lanqiao.util.Result
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/6/12 10:45
     * @Param [localIp]
     * @explain
     */

    @ResponseBody
    @RequestMapping("/getAct")
    public  Result  getAct(String localIp){
        List<Activity>allAct=new ArrayList<>();
        try {
             allAct = activityService.getAllsite(localIp);
        }catch (Exception e){
            return Result.defeated("有错误哟");
        }
        return Result.ok(allAct);
    }

    /**
     * @return org.lanqiao.util.Result
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/6/10 20:19
     * @Param [ip]
     * @explain
     */
    @ResponseBody
    @RequestMapping("/getMap")
    public Result map(String ip, HttpServletResponse response) {
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        String map = activityService.getMap(ip);
        return Result.ok(map);
    }

    /**
     * 功能描述
     *
     * @return org.lanqiao.util.Result
     * @author 吴奇
     * @date 2019/5/28
     */
    @ResponseBody
    @RequestMapping("/getActivity")
    public Result listGetActivity(String userEmail,String name, @RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        // 邮箱验证规则
        String regEx = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(userEmail);
        // 字符串是否与正则表达式相匹配
        boolean rs = matcher.matches();
        if (rs == false) {
            Result re = ResultUtil.error(10, "参数异常");
            return re;
        }

        if (!(currentPage > 0)) {
            Result re = ResultUtil.error(10, "参数异常");
            return re;
        }
        PageInfo<AcActivity> ac = activityService.listGetActivity(userEmail,name, currentPage);
        return ResultUtil.success(ac);
    }

    /**
     * @return org.lanqiao.util.Result
     * @Author 刘金渝
     * @Description //TODO
     * @Date 2019/5/27 17:14
     * @Param [activity]
     * @explain
     */

    @ResponseBody
    @RequestMapping("/add")
    public Result addActivity(Activity activity) {
        if (activity.getActivityEndtime() == null || activity.getActivityStarttime() == null || activity.getActivityName() == null
                || activity.getActivitySite() == null || activity.getActivityStatus() == null || activity.getActivityType() == null || activity.getUserId() == null) {
            return Result.defeated("数据不正确");
        } else {
            activity.setActivityId(UUId.getUUID());
            return activityService.addActivity(activity);
        }
    }

    /**
     * 获取所有活动
     *
     * @return
     */
    @RequestMapping("/getAllActivity")
    public Result getAll(String uid, @PathVariable("currentPage") @RequestParam(value = "id", defaultValue = "1") int currentPage) {
        System.out.println("传过来的参数:" + uid);
        System.out.println(uid.equals("null"));
        if (uid.equals("null")) {
            //得到分页的结果对象
            PageHelper.startPage(currentPage, 3);
            List<Map> allActivity = activityService.getAllActivity();
            System.out.println(allActivity);
            PageInfo<Map> pageInfo = new PageInfo<>(allActivity);
            return Result.ok(pageInfo);
        } else {
            //每页记录数3条
            PageHelper.startPage(currentPage, 3);
            List<Map> mapList = activityService.getByNameActivity(uid);
            PageInfo<Map> pageInfo = new PageInfo<>(mapList);
            return Result.ok(pageInfo);
        }

    }

    /**
     * 更新状态为2
     *
     * @param activityId
     * @return
     */
    @RequestMapping("/activity/{activityId}")
    public Result update(@PathVariable("activityId") @NotNull(message = "活动id不可为空") String activityId) {
        System.out.println("activityId:"+activityId);
        activityService.updateAct(activityId);

        return Result.ok(null);
    }

    /**
     * @return
     * @Author 王宇
     * @Description //TODO 根据活动名来进行活动的模糊查询
     * @Date 2019/06/03 16:18
     * @Param
     */
    @ResponseBody
    @RequestMapping("/getActivityName")
    public Result getActivityName(String activityName) {
        return activityService.getActivityName(activityName);
    }

    /**
     * @Author 彭泳寒
     * @Description //TODO 根据活动id来进行活动时间查询
     */
    @RequestMapping(value = "getActivityTime")
    public Result getActivityTime(String activityId) {
        Activity activity = activityService.getActivityTime(activityId);
        return Result.ok(activity);
    }

    @ResponseBody
    @RequestMapping("/update")
    public Result updateAc(String id, String name, String sndtime) {
        if (sndtime == null || id == null || name.length() == 0) {
            return ResultUtil.error(10, "参数错误");
        }
        Pattern p = Pattern.compile("^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$");
        boolean b = p.matcher(sndtime).matches();
        System.out.println(b);
        if (b == false) {
            return ResultUtil.error(10, "参数错误");
        }
        Activity activity = activityService.getActivityTime(id);
        if (activity == null) {
            return ResultUtil.error(10, "参数错误");
        } else {
            activityService.updateAc(id, name, sndtime);
            return ResultUtil.success();
        }
    }
    /**
     *更具活动的id进行查询活动状态
     * @param activityId
     * @return 任春旭
     */
    @RequestMapping(value = "/queryStatus")
    public Result queryStatus(String activityId){
        Activity activity = activityService.queryStatus(activityId);
        return Result.ok(activity);
    }

    /**
     * 根据活动id查询整个活动
     */
    @RequestMapping(value = "getActivityById")
    public Result getActivityById(@RequestParam(value = "activityId") String activityId){
        Activity activity = activityService.getActivityById(activityId);
        return Result.ok(activity);
    }

}
